package
{
	import flash.geom.Vector3D;

	public class Road extends GameObject
	{
		public static var nodeLocations:Vector.<Vector3D> = new <Vector3D>[
			new Vector3D(1, 0, 0), 
			new Vector3D(0, 1, 0),  
			new Vector3D(1, 1, 0),
			new Vector3D(2, 1, 0),
			new Vector3D(1, 2, 0)
		];
		
		public static const SIZE:int = Constants.SQUARE_SIZE * 2;
		
		public function Road(model:GameObjectModel)
		{
			super(model);
		}
		
		override public function onBuyFinished():void{
			this.createPathNodes();
		}
		
		public function createPathNodes():void{
			var i:int = 0, length:int = nodeLocations.length;
			var pathNodesLookup:Object = new Object();
			
			for(i = 0; i < length; i++){
				var offset:Vector3D = nodeLocations[i].clone();
				offset.scaleBy(Constants.SQUARE_SIZE);
				var nodeLocation:Vector3D = this.model.location.add(offset);
				var pathNode:PathNode = new PathNode(nodeLocation);
				var managedPathNode:PathNode = Global.pathManager.findNode(nodeLocation);
				
				if(managedPathNode != null){
					pathNode = managedPathNode;
				} else {
					Global.pathManager.addNode(pathNode);
				}
				
				
				pathNodesLookup[i] = pathNode;
			}
			
			Global.pathManager.addLink(new PathLink(pathNodesLookup[0], pathNodesLookup[2]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[2], pathNodesLookup[0]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[1], pathNodesLookup[2]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[2], pathNodesLookup[1]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[2], pathNodesLookup[3]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[3], pathNodesLookup[2]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[2], pathNodesLookup[4]));
			Global.pathManager.addLink(new PathLink(pathNodesLookup[4], pathNodesLookup[2]));
		}
	
		override public function wakeup(serData:Object):void{
			super.wakeup(serData);
			this.createPathNodes();
			
		}		
		
		override public function get className():String{
			return 'Road';
		}		
		
		public static function createRoad():Road{
			var config:GameObjectConfig = new GameObjectConfig;
			config.location = Global.originVector;
			config.size = new Vector3D(SIZE, SIZE, 0);
			
			return new Road(new GameObjectModel(config));
		}
	}
}